package com.oracle.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.oracle.mapper.StudentMapper;
import com.oracle.pojo.Student;
import com.oracle.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentMapper studentMapper;

    @Override
    @Transactional(readOnly = true ,propagation = Propagation.SUPPORTS)
    public boolean findStudent(String stuNum, String stuPwd) {
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("stu_Num", stuNum).eq("stu_Pwd", stuPwd);
        Student student = studentMapper.selectOne(queryWrapper);

        return student != null;
    }

    @Override
    @Transactional(readOnly = true ,propagation = Propagation.SUPPORTS)
    public IPage<Student> searchStudent(Student student, Integer pageNum, Integer pageSize) {
        IPage<Student> page = new Page<>(pageNum, pageSize, true);
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        if (ObjectUtils.isEmpty(student)){
            IPage<Student> studentIPage = studentMapper.selectPage(page, null);
            return studentIPage;
        }else {
            String stuName = student.getStuName();
            if (!StringUtils.isEmpty(stuName)){
                queryWrapper.like("stu_Name", stuName);
            }

            String stuNum = student.getStuNum();
            if (!StringUtils.isEmpty(stuNum)){
                queryWrapper.like("stu_Num", stuNum);
            }

            String stuMajor = student.getStuMajor();
            if (!StringUtils.isEmpty(stuMajor)){
                queryWrapper.eq("stu_Major", stuMajor);
            }

            String stuGrade = student.getStuGrade();
            if (!StringUtils.isEmpty(stuMajor)){
                queryWrapper.like("stu_Grade", stuGrade);
            }

            String stuTel = student.getStuTel();
            if (!StringUtils.isEmpty(stuTel)){
                queryWrapper.like("stu_Tel", stuTel);
            }

            IPage<Student> studentIPage = studentMapper.selectPage(page, queryWrapper);
            return studentIPage;
        }
    }

    @Override
    @Transactional(readOnly = true ,propagation = Propagation.SUPPORTS)
    public List<Student> findAll() {
        List<Student> list = studentMapper.selectList(null);
        return list;
    }

    @Override
    @Transactional(readOnly = true ,propagation = Propagation.SUPPORTS)
    public Student findOneStudent(Integer id) {
        Student student = studentMapper.selectById(id);
        return student;
    }

    @Override
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
    public int insertStudent(Student student) {
        student.setStuState(1);
        int insert = studentMapper.insert(student);
        return insert;
    }

    @Override
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
    public int updateStudent(Student student) {
        int i = studentMapper.updateById(student);
        return i;
    }

    @Override
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
    public int deleteStudent(List<Integer> stuId) {
        int i = studentMapper.deleteBatchIds(stuId);
        return i;
    }
}
